/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//API.form = API.from || {};

API.form.BasicForm = function(config){
    
    API.apply(this, config, {
        class: 'basic-form'
    }); 
    
    this.items = [];
    this.propertyList = [];
    
    var form = document.createElement('form');    
    form.className = config.class || 'basic-form'; 
    
    for(var i=0,
            len = config.items.length, 
            items = this.items, 
            prop = this.propertyList; 
    i<len;i++){        
        var item = config.items[i];
        var name = item.getName();
        form.appendChild(item.DOM);
        items[name] = item;
        prop[i] = name;
    };
    
    this.DOM = form;
    
};

API.apply(API.form.BasicForm.prototype,{ 

    getForm : function(){
        return this.DOM;
    },
    
    getItems : function(){
        return this.items;
    },
        
    clear : function(){
        this.getForm().reset();
    },
        
    getValues : function(){  

        var values = {};
        
        API.forEachB(this.getForm().elements,function(e){
            values[e.name] = e.value;
        });
        
        return values;

    },

    setValues : function(values){        
        var formItems = this.getItems();
        API.forEachB(this.propertyList, function(prop){
            var item = formItems[prop];
            if(values[prop] !== "undefined"  && !item.ignoreSet)
                item.setValue(values[prop]);
        });
    }
    
}); 